
--[[
	request操作封装
]]

local string_utils = require("mvc.utils.string_utils");


local _M = {};
--[[
	获取请求参数名称
	返回名称数组
]]
local function get_param_names()
	local args = get_params();
	local j = 1;
	local names = {};
	for i in pairs(args) do
		names[j] = i;
		j = j + 1;
	end
	return names;
end

--[[
	返回所有请求参数的table
]]
local function get_params()

	if _M.args ~= nil then
		return _M.args;
	end

	local args = {};
	local request_method = ngx.var.request_method;
	if "GET" == request_method then
		args = ngx.req.get_uri_args();
	elseif "POST" == request_method then
		args = ngx.req.get_post_args();
	end
	
	--合并urlrewrite目标uri参数
	if ngx.ctx["hades_is_rewrite"] then
		local fp = ngx.ctx["hades_forward_params"];
		for i,t in pairs(fp) do
			args[i] = t;
		end
	end

	--缓存请求参数
	_M.args = args;
	
	return args;
end

--[[
	根据名称获取参数的值默认返回类型为string
]]
local function get_param(name)
	assert(name, "参数名称不能为nil");
	return get_params()[name];
end

--[[
	根据名称获取数字类型参数
]]
local function get_number_param(name)
	assert(name, "参数名称不能为nil");
	local v = get_params()[name];
	local n = tonumber(v);
	if n then
		return n;
	end
	return nil;
end

--[[
	获取query_string
]]
local function get_query_string()
	return ngx.var.query_string;
end

--[[
	获取ContentType
]]
local function get_content_type()
	return ngx.var.content_type;
end

--[[
	获取指定@name的cookie值
]]
local function get_cookie(name)
	return get_cookies()[name]
end

--[[
	获取所有的cookie
]]
local function get_cookies()
	local cookies = {};
	local cookie_str = string.gsub(get_header("Cookie"), " ", "");
	local str_arr = string_utils.split(cookie_str, ";");
	local it_arr;
	for i = 1, #str_arr, 1 do
		it_arr = string_utils.split(str_arr[i], "=");
		if #it_arr == 2 then
			cookies[it_arr[1]] = it_arr[2];
		end
	end
	return cookies;
end

--[[
	获取头信息
]]
local function get_header(name)
	---ngx.req.get_headers()[name]
end

--[[
	获取数字类型头信息
]]
local function get_int_header(name)
	return tonumber(ngx.req.get_headers()[name]);
end

--[[
	获取日期类型头信息
]]
local function get_date_header(name)
	
end

--[[
	获取所有头信息
]]
local function get_headers()
	return ngx.req.get_headers();
end

--[[
	获取客户端IP
]]
local function get_remote_addr()
	return ngx.var.remote_addr;
end

--[[
	获取客户端访问服务端的端口
]]
local function get_remote_port()
	return ngx.var.remote_port;
end

--[[
	获取请求信息中的Host
]]
local function get_host()
	return ngx.var.host;
end

--[[
	获取请求协议http OR https
]]
local function get_scheme()
	return ngx.var.scheme;
end

--[[
	获取User-Agent
]]
local function get_user_agent()
	return ngx.var.http_user_agent;
end

--[[
	将键值对数据设置到请求上下文中
]]
local function set_attribute(name, value)
	ngx.var[name] = value;
end

--[[
	从请求上下文中获取数据
]]
local function get_attribute(name)
	return ngx.var[name];
end

--[[
	从请求上下文中获取数据
]]
local function is_ajax()
	local x = ngx.header["x-requested-with"];
	return x == "XMLHttpRequest";
end


_M.get_param_names = get_param_names;
_M.get_params = get_params;
_M.get_param = get_param;
_M.get_number_param = get_number_param;
_M.get_query_string = get_query_string;
_M.get_content_type = get_content_type;
_M.get_cookie = get_cookie;
_M.get_cookies = get_cookies;
_M.get_header = get_header;
_M.get_int_header = get_int_header;
_M.get_date_header = get_date_header;
_M.get_headers = get_headers;
_M.get_remote_addr = get_remote_addr;
_M.get_remote_port = get_remote_port;
_M.get_host = get_host;
_M.get_scheme = get_scheme;
_M.get_user_agent = get_user_agent;
_M.set_attribute = set_attribute;
_M.get_attribute = get_attribute;
_M.is_ajax = is_ajax;


return _M;

